package com.android.fragmentdemo.defineviews;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class DynamicContainer2ViewGroup extends ViewGroup {
    private static final String TAG = "DynamicContainer2ViewGroup";
    private final Context context;
    int totalHeight = 0;
    private boolean lessScreenHeight = true;

    public DynamicContainer2ViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        //lessScreenHeight = totalHeight <= getMeasuredHeight();
        Log.d(TAG,
                "onMeasure--childCount:" + childCount + ",getMeasuredHeight():" + getMeasuredHeight()
                + ",totalHeight:" + totalHeight + ",lessScreenHeight:" + lessScreenHeight);
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
            MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
            //int leftMargin = layoutParams.leftMargin;
            int topMargin = layoutParams.topMargin;
            //int rightMargin = layoutParams.rightMargin;
            int bottomMargin = layoutParams.bottomMargin;
            totalHeight += topMargin + bottomMargin + child.getMeasuredHeight();
            //Log.d(TAG, "onMeasure---totalHeight:" + totalHeight);
        }
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int tempTotalHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            Log.d(TAG, "onLayout---childWidth:" + childWidth + ",childHeight:" + childHeight);
            MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
            int leftMargin = layoutParams.leftMargin;
            int topMargin = layoutParams.topMargin;
            //int rightMargin = layoutParams.rightMargin;
            int bottomMargin = layoutParams.bottomMargin;
            child.layout(leftMargin, tempTotalHeight + topMargin, leftMargin + childWidth, totalHeight + topMargin + childHeight);
            tempTotalHeight += topMargin + childHeight + bottomMargin;
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(context, attrs);
    }

    public boolean shouldAddView() {
        return lessScreenHeight;
    }
}
